package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

public class AppResult<T> {

    @Getter
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论何种情况都参与json序列化
    private int code;
    @Getter
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论何种情况都参与json序列化
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论何种情况都参与json序列化
    @Getter
    private T data;

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public AppResult(int code, String message) {
        this(code, message, null);
    }

    //success
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.messagesg);
    }

    public static AppResult success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    //泛型方法
    public static <T> AppResult<T> success(T data) {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessagesg(), data);
    }

    //
    public static <T> AppResult<T> success(String message, T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    //failed
    public static AppResult failed() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessagesg());
    }

    public static AppResult failed(AppResult appResult) {
        return appResult;
    }

    public static AppResult failed(String errMsg) {
        return new AppResult(ResultCode.FAILED.getCode(), errMsg);
    }

    public static AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(), resultCode.getMessagesg());
    }
}
